<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>circularbuffer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_structs_circularbuffer.js.html">circularbuffer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview Datastructure: Circular Buffer.
<a name="line18"></a> *
<a name="line19"></a> * Implements a buffer with a maximum size. New entries override the oldest
<a name="line20"></a> * entries when the maximum size has been reached.
<a name="line21"></a> *
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.structs.CircularBuffer&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Class for CircularBuffer.
<a name="line31"></a> * @param {number=} opt_maxSize The maximum size of the buffer.
<a name="line32"></a> * @constructor
<a name="line33"></a> */
<a name="line34"></a>goog.structs.CircularBuffer = function(opt_maxSize) {
<a name="line35"></a>  /**
<a name="line36"></a>   * Maximum size of the the circular array structure.
<a name="line37"></a>   * @type {number}
<a name="line38"></a>   * @private
<a name="line39"></a>   */
<a name="line40"></a>  this.maxSize_ = opt_maxSize || 100;
<a name="line41"></a>
<a name="line42"></a>  /**
<a name="line43"></a>   * Underlying array for the CircularBuffer.
<a name="line44"></a>   * @type {Array}
<a name="line45"></a>   * @private
<a name="line46"></a>   */
<a name="line47"></a>  this.buff_ = [];
<a name="line48"></a>};
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * Index of the next element in the circular array structure.
<a name="line53"></a> * @type {number}
<a name="line54"></a> * @private
<a name="line55"></a> */
<a name="line56"></a>goog.structs.CircularBuffer.prototype.nextPtr_ = 0;
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * Adds an item to the buffer. May remove the oldest item if the buffer is at
<a name="line61"></a> * max size.
<a name="line62"></a> * @param {*} item The item to add.
<a name="line63"></a> */
<a name="line64"></a>goog.structs.CircularBuffer.prototype.add = function(item) {
<a name="line65"></a>  this.buff_[this.nextPtr_] = item;
<a name="line66"></a>  this.nextPtr_ = (this.nextPtr_ + 1) % this.maxSize_;
<a name="line67"></a>};
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Returns the item at the specified index.
<a name="line72"></a> * @param {number} index The index of the item. The index of an item can change
<a name="line73"></a> *     after calls to {@code add()} if the buffer is at maximum size.
<a name="line74"></a> * @return {*} The item at the specified index.
<a name="line75"></a> */
<a name="line76"></a>goog.structs.CircularBuffer.prototype.get = function(index) {
<a name="line77"></a>  index = this.normalizeIndex_(index);
<a name="line78"></a>  return this.buff_[index];
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Sets the item at the specified index.
<a name="line84"></a> * @param {number} index The index of the item. The index of an item can change
<a name="line85"></a> *     after calls to {@code add()} if the buffer is at maximum size.
<a name="line86"></a> * @param {*} item The item to add.
<a name="line87"></a> */
<a name="line88"></a>goog.structs.CircularBuffer.prototype.set = function(index, item) {
<a name="line89"></a>  index = this.normalizeIndex_(index);
<a name="line90"></a>  this.buff_[index] = item;
<a name="line91"></a>};
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Returns the current number of items in the buffer.
<a name="line96"></a> * @return {number} The current number of items in the buffer.
<a name="line97"></a> */
<a name="line98"></a>goog.structs.CircularBuffer.prototype.getCount = function() {
<a name="line99"></a>  return this.buff_.length;
<a name="line100"></a>};
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * @return {boolean} Whether the buffer is empty.
<a name="line105"></a> */
<a name="line106"></a>goog.structs.CircularBuffer.prototype.isEmpty = function() {
<a name="line107"></a>  return this.buff_.length == 0;
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Empties the current buffer.
<a name="line113"></a> */
<a name="line114"></a>goog.structs.CircularBuffer.prototype.clear = function() {
<a name="line115"></a>  this.buff_.length = 0;
<a name="line116"></a>  this.nextPtr_ = 0;
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * @return {Array} The values in the buffer.
<a name="line122"></a> */
<a name="line123"></a>goog.structs.CircularBuffer.prototype.getValues = function() {
<a name="line124"></a>  // getNewestValues returns all the values if the maxCount parameter is the
<a name="line125"></a>  // count
<a name="line126"></a>  return this.getNewestValues(this.getCount());
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Returns the newest values in the buffer up to {@code count}.
<a name="line132"></a> * @param {number} maxCount The maximum number of values to get. Should be a
<a name="line133"></a> *     positive number.
<a name="line134"></a> * @return {Array} The newest values in the buffer up to {@code count}.
<a name="line135"></a> */
<a name="line136"></a>goog.structs.CircularBuffer.prototype.getNewestValues = function(maxCount) {
<a name="line137"></a>  var l = this.getCount();
<a name="line138"></a>  var start = this.getCount() - maxCount;
<a name="line139"></a>  var rv = [];
<a name="line140"></a>  for (var i = start; i &lt; l; i++) {
<a name="line141"></a>    rv[i] = this.get(i);
<a name="line142"></a>  }
<a name="line143"></a>  return rv;
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * @return {Array} The indexes in the buffer.
<a name="line149"></a> */
<a name="line150"></a>goog.structs.CircularBuffer.prototype.getKeys = function() {
<a name="line151"></a>  var rv = [];
<a name="line152"></a>  var l = this.getCount();
<a name="line153"></a>  for (var i = 0; i &lt; l; i++) {
<a name="line154"></a>    rv[i] = i;
<a name="line155"></a>  }
<a name="line156"></a>  return rv;
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Whether the buffer contains the key/index.
<a name="line162"></a> * @param {number} key The key/index to check for.
<a name="line163"></a> * @return {boolean} Whether the buffer contains the key/index.
<a name="line164"></a> */
<a name="line165"></a>goog.structs.CircularBuffer.prototype.containsKey = function(key) {
<a name="line166"></a>  return key &lt; this.getCount();
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Whether the buffer contains the given value.
<a name="line172"></a> * @param {*} value The value to check for.
<a name="line173"></a> * @return {boolean} Whether the buffer contains the given value.
<a name="line174"></a> */
<a name="line175"></a>goog.structs.CircularBuffer.prototype.containsValue = function(value) {
<a name="line176"></a>  var l = this.getCount();
<a name="line177"></a>  for (var i = 0; i &lt; l; i++) {
<a name="line178"></a>    if (this.get(i) == value) {
<a name="line179"></a>      return true;
<a name="line180"></a>    }
<a name="line181"></a>  }
<a name="line182"></a>  return false;
<a name="line183"></a>};
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>/**
<a name="line187"></a> * Returns the last item inserted into the buffer.
<a name="line188"></a> * @return {*} The last item inserted into the buffer, or null if the buffer is
<a name="line189"></a> *     empty.
<a name="line190"></a> */
<a name="line191"></a>goog.structs.CircularBuffer.prototype.getLast = function() {
<a name="line192"></a>  if (this.getCount() == 0) {
<a name="line193"></a>    return null;
<a name="line194"></a>  }
<a name="line195"></a>  return this.get(this.getCount() - 1);
<a name="line196"></a>};
<a name="line197"></a>
<a name="line198"></a>
<a name="line199"></a>/**
<a name="line200"></a> * Helper function to convert an index in the number space of oldest to
<a name="line201"></a> * newest items in the array to the position that the element will be at in the
<a name="line202"></a> * underlying array.
<a name="line203"></a> * @param {number} index The index of the item in a list ordered from oldest to
<a name="line204"></a> *     newest.
<a name="line205"></a> * @return {number} The index of the item in the CircularBuffer&#39;s underlying
<a name="line206"></a> *     array.
<a name="line207"></a> * @private
<a name="line208"></a> */
<a name="line209"></a>goog.structs.CircularBuffer.prototype.normalizeIndex_ = function(index) {
<a name="line210"></a>  if (index &gt;= this.buff_.length) {
<a name="line211"></a>    throw Error(&#39;Out of bounds exception&#39;);
<a name="line212"></a>  }
<a name="line213"></a>
<a name="line214"></a>  if (this.buff_.length &lt; this.maxSize_) {
<a name="line215"></a>    return index;
<a name="line216"></a>  }
<a name="line217"></a>
<a name="line218"></a>  return (this.nextPtr_ + Number(index)) % this.maxSize_;
<a name="line219"></a>};
</pre>


</body>
</html>
